# 交易
import sys
import time

class TraderHandle:
    def __init__(self):
        self.last_time = time.time()
        self.expire_time = 10.0

    def handle_buy(self, data):
        # 获取及资金帐户信息
        # 进行风控策略检查
        # 下单
        print("处理bug:" + data)

    def handle_sell(self, data):
        print("处理信号:" + data)

    def run(self, queue_object):
        
        while True:
            # 进程大于10 就退出
            ts_diff = (time.time() - self.last_time) * 1
            print("当前时间间隔：" + str(ts_diff))
            if ts_diff > self.expire_time:
                print("进程长时间无任务，退出")
                print("时间:" + str(time.time()) + ' : ' + str(self.last_time) + ' : ' + str(ts_diff))
                sys.exit(0)

            if queue_object.empty():
                print("队列中无数据，等待中")
                time.sleep(1)
            else:
                self.last_time = time.time()
                item = queue_object.get()  # Get an item from the queue
                queue_object.task_done()  # Signal that the item has been processed
                data = item['data'].iloc[0]
                print(data)
                if data['signal'] == 1:
                    self.handle_buy(data)
                elif data['signal'] == -1:
                    self.handle_sell(data)
                else:
                    print("无信号")


